<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加流程</title>
</head>
<body class="layui-layout-body">
    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div class="body_right">
            <h4 class="nav_title">审核流程修改</h4>
            
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			  <legend>基本信息</legend>
			</fieldset>
            <form class="layui-form"  action="">
            <input type="hidden" name="selectedIds" id="selectedIds" value="<#list workflow.settings as item><#if item_index gt 0>,</#if>${item.partyId}</#list>">
            <input type="hidden" name="workflowId" value="${workflow.workflowId}">
            <div class="layui-form-item">
		    		<label class="layui-form-label">流程名称</label>
		        <div class="layui-input-inline">
		            <input type="text" name="name" value="${workflow.name}" lay-verify="name" autocomplete="off" placeholder="请输入流程名称" class="layui-input">
		        </div>
			</div>
			<div class="layui-form-item">
	            <label class="layui-form-label">审核类型</label>
	            <div class="layui-input-inline">
	                <select name="workflowType">
	                		<option value="JOIN" <#if workflow.workflowType == 'JOIN'>selected</#if> >联合审核</option>
	                     <option value="QUEUE" <#if workflow.workflowType == 'QUEUE'>selected</#if> >顺序审核</option>
	                    
	                </select>
	            </div>
	        </div>
	        	<div class="layui-form-item">
	        		<label class="layui-form-label">流程描述</label>
	            <div class="layui-input-inline" style="width:250px">
	                <textarea name="description" placeholder="描述信息 可以为空" class="layui-textarea">${workflow.description}</textarea>
	            </div>
	        	</div>
			
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			  <legend>审核人员</legend>
			</fieldset>
			<blockquote class="layui-elem-quote">
			  顺序审核类型需设置顺序大小，顺序审核数字按从小到大审核： <a href="javascript:openwinx('选择人员','/workflow/staffs.htm','800','500')" class="add_btn">添加</a>  
			</blockquote>
			<table class="layui-table" lay-even="" lay-skin="nob">
			  <thead>
			    <tr>
			      <th>姓名</th>
			      <th>职务</th>
			      <th>顺序</th>
			      <th>操作</th>
			    </tr>
			  </thead>
			  <tbody id="trItem">
			  	<#list workflow.settings as item>
			  		<tr id="tr_${item.partyId}">
				      <td><input type="hidden"  name="partyId" value="${item.partyId}">${item.person.name}</td>
				      <td>${item.person.position}</td>
				      <td><div class="layui-input-inline" style="width:50px"><input type="text" name="sequence" value="${item.sequence}" required="" class="layui-input"></div></td>
				      <td><a href="javascript:del('${item.partyId}');">删除</a></td>
				    </tr>
			  	</#list>	
			  	
			  </tbody>
			</table> 
			
			<div style="text-align: center;">
	            	<button class="layui-btn" lay-submit="" lay-filter="add">提交</button>
	        	</div>
			
			</form>
        </div>
     </div>
     
     <script>
     
     	function del(id){
     	
     		layer.confirm('确定是否删除', {btn: ['确定','取消'] }, function(){
				
			    
			    var $selectedIds = $("#selectedIds");
	     		var selectedIds = $selectedIds.val();
	     		
	     		idArray =  selectedIds ==''?new Array():selectedIds.split(",");
				var index = idArray.indexOf(id+"");
				
	     		if (index > -1) {
				   idArray.splice(index, 1);
				   
				   console.log(idArray);
				}
				
				$selectedIds.val(idArray.join(","));
				
				$("#tr_"+id).remove();
			    layer.msg('删除成功', {icon: 1});
				
			});
     		
     		return false;
     	}
     
        //JavaScript代码区域
        layui.use(['element','form','layedit','laypage'], function(){
            var element = layui.element;
            var form = layui.form;
            var   layer = layui.layer;
            
          	
            var settingStr ="";
          //监听提交
            form.on('submit(add)', function(data){
             	var partyIds = $("input[name='partyId']");
	            var sequences = $("input[name='sequence']");
	            partyIds.each(function(index,item){
	            		
	            		if(index > 0 ){
	            			settingStr+=","
	            		}
	            		settingStr += $(this).val()+"|"+$(sequences.get(index)).val();
	            		
	            })
            		
            		var field = data.field;
            		field.settingStr = settingStr;
            		
            	   $.ajax({
					type:"POST", 
					url:"/workflow/modify.htm",
					data:field,
					success:function(result) {
						if(result.code=='1'){
							layer.alert("修改成功", {icon: 6},function () {
				           // 获得frame索引
				            		location.href="/workflow/list.htm";
				       		});
						}else{
						 	layer.msg(result.msg);
						}
				 	} 
				});
           
                return false;
            });
        });
    </script>
</body>
</html>